package cn.edu.scau.cmi.xuandingcong.client;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import cn.edu.scau.cmi.xuandingcong.composite.consistent.Component;
import cn.edu.scau.cmi.xuandingcong.composite.consistent.Person;
import cn.edu.scau.cmi.xuandingcong.composite.consistent.Team;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ConsistentCompositeClient extends Application {
	private static TreeView<String> treeView = new TreeView<String>();
	private List<Team> teams = new ArrayList<>();

	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		HBox root = new HBox();
		VBox inputBox = new VBox();
		HBox btn = new HBox();
		btn.setCenterShape(true);
		teams.add(new Team("Component"));
		show();

		Insets insets = new Insets(10);
		Insets insets2 = new Insets(40, 50, 0, 50);
		Insets insets3 = new Insets(25, 5, 0, 30);
		Label label = new Label("请输入成员或团队:");
		TextField input = new TextField();
		Button addPersonBtn = new Button("添加成员");
		Button addTeamBtn = new Button("添加团队");
		Button deleteBtn = new Button("删       除");
		addPersonBtn.setPadding(insets);
		addTeamBtn.setPadding(insets);
		deleteBtn.setPadding(insets);
		btn.setPadding(insets3);
		inputBox.setPadding(insets2);
		btn.getChildren().addAll(addPersonBtn, addTeamBtn, deleteBtn);
		inputBox.getChildren().addAll(label, input, btn);
		root.getChildren().addAll(treeView, inputBox);

		Scene scene = new Scene(root);
		primaryStage.setScene(scene);
		primaryStage.setTitle("一致性组合模式");
		primaryStage.show();

		addPersonBtn.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				int index = treeView.getSelectionModel().getSelectedIndex();

				TreeItem choice = treeView.getTreeItem(index);
				String name = input.getText().trim();
				Person person = new Person(name);
				for (Team team : teams) {
					if (team.getName().equals(choice.getValue())) {
						team.addChild(person);
						break;
					}
					addPerson(choice, person, team.getChild());
				}
				show();

			}

			private void addPerson(TreeItem choice, Person person, List<Component> list) {

				for (Component component : list) {
					if (component.getClass().equals(Team.class)) {
						Team team = (Team) component;
						if (team.getName().equals(choice.getValue())) {
							team.addChild(person);
							break;
						}
						addPerson(choice, person, team.getChild());
					}
				}
			}
		});

		addTeamBtn.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {

				int index = treeView.getSelectionModel().getSelectedIndex();

				TreeItem choice = treeView.getTreeItem(index);
				String teamName = input.getText().trim();
				Team team = new Team(teamName);
				Team t = teams.get(0);
				if (t.getName().equals(choice.getValue()))
					t.addChild(team);
				else
					addTeam(choice, team, t.getChild());
				show();

			}

			private void addTeam(TreeItem choice, Team team, List<Component> teams) {
				for (Component t : teams) {
					if (t.getClass().equals(Team.class)) {
						Team tt = (Team) t;
						if (tt.getName().equals(choice.getValue())) {
							tt.addChild(team);
							break;
						}
						addTeam(choice, team, tt.getChild());
					}
				}
			}
		});

		deleteBtn.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				int index = treeView.getSelectionModel().getSelectedIndex();

				TreeItem choice = treeView.getTreeItem(index);
				String name = choice.getValue().toString();
				for (Team team : teams) {
					if (team.getName().equals(name)) {
						teams.remove(team);
						break;
					}
					for (int i = 0; i < team.getSize(); i++) {
						if (team.getChild().get(i).getClass().equals(Person.class)) {
							Person p = (Person) team.getChild().get(i);
							if (p.getName().equals(name)) {
								team.deleteChild(team.getChild().get(i));
								break;
							}
						} else if (team.getChild().get(i).getClass().equals(Team.class)) {
							findItem(name, team.getChild());
						}
					}
				}
				Alert alert = new Alert(AlertType.INFORMATION);
				alert.titleProperty().set("信息");
				alert.headerTextProperty().set("删除成功！");
				alert.showAndWait();
				show();

			}

			void findItem(String name, List<Component> teams) {
				for (int i = 0; i < teams.size(); i++) {
					if (teams.get(i).getClass().equals(Person.class)) {
						Person p = (Person) teams.get(i);
						if (p.getName().equals(name)) {
							teams.remove(teams.get(i));
							break;
						}
					} else if (teams.get(i).getClass().equals(Team.class)) {
						Team t = (Team) teams.get(i);
						if (t.getName().equals(name)) {
							teams.remove(teams.get(i));
							break;
						}
						for (int j = 0; j < t.getSize(); j++) {
							if (t.getChild().get(j).getClass().equals(Person.class)) {
								Person p = (Person) t.getChild().get(j);
								if (p.getName().equals(name)) {
									t.deleteChild(t.getChild().get(j));
									break;
								}
							} else if (t.getChild().get(j).getClass().equals(Team.class)) {
								findItem(name, t.getChild());
							}
						}
					}
				}
			}
		});

	}

	private void show() {
		TreeItem<String> root = new TreeItem<>("Organize");
		root.setExpanded(true);
		treeView.setRoot(root);
		for (Team team : teams) {
			TreeItem<String> rootItem = new TreeItem<>(team.getName());
			rootItem.setExpanded(true);
			for (Component organization : team.getChild()) {
				if (organization.getClass().equals(Person.class)) {
					Person p = (Person) organization;
					TreeItem<String> item = new TreeItem(p.getName());
					item.setExpanded(true);
					rootItem.getChildren().add(item);
				} else if (organization.getClass().equals(Team.class)) {
					showTree(rootItem, team.getChild());
				}
			}
			root.getChildren().add(rootItem);
		}
	}

	private void showTree(TreeItem<String> root, List<Component> teams) {
		for (Component team : teams) {
			TreeItem<String> rootItem = new TreeItem<>(team.getName());
			rootItem.setExpanded(true);
			if (team.getClass().equals(Person.class)) {
				Person p = (Person) team;
				TreeItem<String> item = new TreeItem(p.getName());
				item.setExpanded(true);
				rootItem.getChildren().add(item);
			} else if (team.getClass().equals(Team.class)) {
				Team t = (Team) team;
				for (Component organization : t.getChild()) {
					if (organization.getClass().equals(Person.class)) {
						Person p = (Person) organization;
						TreeItem<String> item = new TreeItem(p.getName());
						item.setExpanded(true);
						rootItem.getChildren().add(item);
					} else if (organization.getClass().equals(Team.class)) {
						showTree(rootItem, t.getChild());
					}
				}
				root.getChildren().add(rootItem);
			}
		}
	}

}
